{block name=body}
<div class="row">
    <div class="col-sm-12">
        <form class="well form-horizontal" id="Edit_Record" accept-charset="UTF-8" method="post" data-callback="" data-element="">
            <div class="row">
                <div class="col-sm-12 edit-page-header">
                    <span class="edit-page-title">{if $fields.franchisee_id neq ""}编辑{else}新建{/if} {$zone}</span> {if $cnt eq ""}{assign var=cnt value=0}{/if}
                    <a href="javascript:void(0);" onClick="$('#Edit_Record').submit();" class="btn btn-primary pull-right top-btn"><span class="glyphicon glyphicon-floppy-saved"></span> 保存</a>
                    <div class="published" {if $fields.franchisee_published eq 0}style="display:none;" {/if}>
                        <!-- 已发布 -->
                        <a href="javascript:void(0);" onClick="saveDraft('field[1][tbl_franchisee][{$cnt}][id]','franchisee_object_id','franchisee_published','field[1][tbl_franchisee][{$cnt}][franchisee_deleted]',false);" class="btn btn-info pull-right top-btn published"><span class="glyphicon glyphicon-floppy-disk"></span> 保存草稿</a>
                        <a href="javascript:void(0);" onClick="unpublish('franchisee_published');" class="btn btn-warning pull-right top-btn"><span class="glyphicon glyphicon-thumbs-down"></span> 不发布</a>
                    </div>
                    <div class="drafts" {if $fields.franchisee_published eq 1}style="display:none;" {/if}>
                        <!-- 草稿 -->
                        <a href="javascript:void(0);" onClick="publish('field[1][tbl_franchisee][{$cnt}][id]','franchisee_object_id','franchisee_published','field[1][tbl_franchisee][{$cnt}][franchisee_deleted]');" class="btn btn-primary pull-right top-btn drafts"><span class="glyphicon glyphicon-thumbs-up"></span> 保存 &amp; 发布</a>
                    </div>
                    <input type="hidden" value="franchisee_id" name="primary_id" id="primary_id" />
                    <input type="hidden" value="franchisee_id" name="field[1][tbl_franchisee][{$cnt}][id]" id="id" />
                    <input type="hidden" value="{$fields.franchisee_id}" name="field[1][tbl_franchisee][{$cnt}][franchisee_id]" id="franchisee_id" class="key">
                    <input type="hidden" value="{if $fields.franchisee_object_id}{$fields.franchisee_object_id}{else}{$objID}{/if}" name="field[1][tbl_franchisee][{$cnt}][franchisee_object_id]" id="franchisee_object_id">
                    <input type="hidden" value="{if $fields.franchisee_created}{$fields.franchisee_created}{else}{'Y-m-d H:i:s'|date}{/if}" name="field[1][tbl_franchisee][{$cnt}][franchisee_created]" id="franchisee_created">
                    <input type="hidden" value="{$fields.franchisee_published}" name="field[1][tbl_franchisee][{$cnt}][franchisee_published]" id="franchisee_published">
                    <input type="hidden" name="formToken" id="formToken" value="{$token}" />
                </div>
            </div>
            <div class="row published" {if $fields.franchisee_published eq 0}style="display:none;" {/if}>
                <div class="alert alert-success text-center">
                    <strong>已发布</strong>
                </div>
            </div>
            <div class="row drafts" {if $fields.franchisee_published eq 1}style="display:none;" {/if}>
                <div class="alert alert-info text-center">
                    <strong>草稿</strong>
                </div>
            </div>
            <ul class="nav nav-tabs" id="myTab">
                <li class="active"><a href="#details" data-toggle="tab">详情</a></li>
                <li><a href="#partners" data-toggle="tab">用户管理</a></li>
                <li><a href="#log" data-toggle="tab">日志</a></li>
            </ul>
            <div class="tab-content">
                <!--===+++===+++===+++===+++===+++ DETAILS TAB +++===+++===+++===+++===+++====-->
                <div class="tab-pane active" id="details">
                    <div class="row form" data-error="<b>详情</b>选项上发现错误。打开<b>详情</b>选项以查看特定错误通知。">
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="id_franchisee_name">名称 *</label>
                            <div class="col-sm-5">
                                <input class="form-control" type="text" value="{$fields.franchisee_name}" name="field[1][tbl_franchisee][{$cnt}][franchisee_name]" id="id_franchisee_name" required>
                                <span class="help-block"></span>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="id_franchisee_description">介绍</label>
                            <div class="col-sm-5">
                                <textarea name="field[1][tbl_franchisee][{$cnt}][franchisee_description]" id="id_franchisee_description" class="tinymce">{$fields.franchisee_description}</textarea>
                            </div>
                        </div>
                        <!-- <div class="row form-group">
								<label class="col-sm-3 control-label" for="franchisee_image">Image<br>
								<small>尺寸: 350px宽 * 350px高 <br>(optional)</small></label>
							<div class="col-sm-9">
								<input type="hidden" value="{$fields.franchisee_image}" name="field[1][tbl_franchisee][{$cnt}][franchisee_image]" id="franchisee_image_link" class="fileinput"> 
								<span class="file-view" id="franchisee_image_path"> {if $fields.franchisee_image}<a href="{$fields.franchisee_image}" target="_blank" >查看</a>{else}暂无{/if} </span> 
								<a href="javascript:void(0);" class="btn btn-info marg-5r" onclick="getFileType('franchisee_image','','');">选择文件</a> 
								<a href="javascript:void(0);" class="btn btn-info" onclick="$('#franchisee_image_link').val('');$('#franchisee_image_path').html('None');">移除文件</a>
							</div>
						</div> -->
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="id_franchisee_enrolment_target">招生人数</label>
                            <div class="col-sm-5">
                                <input class="form-control number" type="text" value="{$fields.franchisee_enrolment_target}" name="field[1][tbl_franchisee][{$cnt}][franchisee_enrolment_target]" id="id_franchisee_enrolment_target">
                            </div>
                        </div>
                        <!-- <div class="row form-group">
							<label class="col-sm-3 control-label" for="id_franchisee_abn">ABN</label>
							<div class="col-sm-5">
								<input class="form-control" type="text" value="{$fields.franchisee_abn}" name="field[1][tbl_franchisee][{$cnt}][franchisee_abn]" id="id_franchisee_abn" >
							</div>
						</div> -->
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="id_franchisee_state">省份 *</label>
                            <div class="col-sm-5">
                                <select class="form-control required" name="field[1][tbl_franchisee][{$cnt}][franchisee_state]" onblur="fillLocation();" id="id_franchisee_state">
                                    <option value="">请选择</option> {foreach $fields.options.state as $opt}
                                    <option value="{$opt.value}" {if $fields.franchisee_state eq $opt.value}selected="selected"{/if} data-id="{$opt.id}">{$opt.value}</option>
                                    {/foreach}
                                </select>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="id_franchisee_suburb">城市 *</label>
                            <div class="col-sm-5">
                                <select class="form-control required" name="field[1][tbl_franchisee][{$cnt}][franchisee_suburb]" onblur="fillLocation();" id="id_franchisee_suburb">
                                    <option value="">请选择</option>
                                </select>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="id_franchisee_country">区县 *</label>
                            <div class="col-sm-5">
                                <select class="form-control required" name="field[1][tbl_franchisee][{$cnt}][franchisee_country]" onblur="fillLocation();" id="id_franchisee_country">
                                    <option value="">请选择</option>
                                </select>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="id_franchisee_street">详细地址</label>
                            <div class="col-sm-5">
                                <input class="form-control" type="text" value="{$fields.franchisee_street}" onchange="fillLocation();" name="field[1][tbl_franchisee][{$cnt}][franchisee_street]" id="id_franchisee_street">
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="id_franchisee_phone">电话</label>
                            <div class="col-sm-5">
                                <input class="form-control" type="text" value="{$fields.franchisee_phone}" name="field[1][tbl_franchisee][{$cnt}][franchisee_phone]" id="id_franchisee_phone">
                            </div>
                        </div>
                    </div>
                </div>
                <!--===+++===+++===+++===+++===+++ USERS TAB +++===+++===+++===+++===+++====-->
                <div class="tab-pane" id="partners">
                    <div class="row form" id="partners-wrapper" data-error="Error found on <b>Users tab</b>. View <b>Users tab</b> to see specific error notices.">
                        {assign var='partnerno' value=0} {foreach $fields.users as $users} {assign var='partnerno' value=$partnerno+1} {include file='franchise_users.tpl'} {/foreach}
                    </div>
                    <div class="row btn-inform">
                        <a href="javascript:void(0);" class="btn btn-success btn-add-new" onclick="$('.partners').slideUp();newPartner();"> 添加新用户</a>
                    </div>
                    <input type="hidden" value="{$partnerno}" id="partnerno">
                </div>
                <!--===+++===+++===+++===+++===+++ LOG TAB +++===+++===+++===+++===+++====-->
                <div class="tab-pane" id="log">
                    <div class="row form" id="tags-wrapper">
                        <div class="col-sm-12">
                            {if $fields.logs}
                            <table class="table table-bordered table-striped table-hover">
                                <thead>
                                    <tr>
                                        <th>时间</th>
                                        <th>操作</th>
                                        <th>用户</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {foreach $fields.logs as $log}
                                    <tr {if $log.franchisee_id eq $fields.franchisee_id}class="info" {/if}>
                                        <td>{$log.log_created|date_format:"%d/%b/%Y %r"}</td>
                                        <td>{$log.log_action}{if $log.log_action eq 'Add' || $log.log_action eq 'Delete'} draft{/if}</td>
                                        <td>{$log.admin_name}</td>
                                    </tr>
                                    {/foreach}
                                </tbody>
                            </table>
                            {else} 暂无日志。 {/if}
                        </div>
                    </div>
                </div>
            </div>
            <div class="row form-group form-bottom-btns">
                <a href="javascript:void(0);" onClick="$('#Edit_Record').submit();" class="btn btn-primary pull-right top-btn"><span class="glyphicon glyphicon-floppy-saved"></span> 保存</a>
                <div class="published" {if $fields.franchisee_published eq 0}style="display:none;" {/if}>
                    <!-- 已发布 -->
                    <a href="javascript:void(0);" onClick="saveDraft('field[1][tbl_franchisee][{$cnt}][id]','franchisee_object_id','franchisee_published','field[1][tbl_franchisee][{$cnt}][franchisee_deleted]', false);" class="btn btn-info pull-right top-btn published"><span class="glyphicon glyphicon-floppy-disk"></span> 保存草稿</a>
                    <a href="javascript:void(0);" onClick="unpublish('franchisee_published');" class="btn btn-warning pull-right top-btn"><span class="glyphicon glyphicon-thumbs-down"></span> 不发布</a>
                </div>
                <div class="drafts" {if $fields.franchisee_published eq 1}style="display:none;" {/if}>
                    <!-- 草稿 -->
                    <a href="javascript:void(0);" onClick="publish('field[1][tbl_franchisee][{$cnt}][id]','franchisee_object_id','franchisee_published','field[1][tbl_franchisee][{$cnt}][franchisee_deleted]');" class="btn btn-primary pull-right top-btn drafts"><span class="glyphicon glyphicon-thumbs-up"></span> 保存 &amp; 发布</a>
                </div>
            </div>
        </form>
    </div>
</div>
{include file='jquery-validation.tpl'}
<script type="text/javascript">
$(document).ready(function() {
	var oProvince = $('#id_franchisee_state');
	var oCity = $('#id_franchisee_suburb');
	var oCountry = $('#id_franchisee_country');
	var temp_html;

    var city = function(){
        temp_html = ""; 
        var n = oProvince.get(0).selectedIndex;
        var id = oProvince.find("option:selected").data("id");
        $.ajax({
            type: "POST",
            url: "/admin/includes/processes/get-location.php",
            cache: false,
            data: {
            	type:"city",
            	id:id
            },
            dataType: "json",
            success: function(data) {
                console.log(data);
	        	temp_html = "";
	            for(var i in data){
	            	temp_html+="<option value='"+i+"' data-id='"+data[i]+"'>"+i+"</option>";
	            }
	            oCity.html(temp_html);
                district();
            }
        });
    };

    var district = function(){
        temp_html = ""; 
        var n = oCity.get(0).selectedIndex;
        var id = oCity.find("option:selected").data("id");
        $.ajax({
            type: "POST",
            url: "/admin/includes/processes/get-location.php",
            cache: false,
            data: {
            	type:"country",
            	id:id
            },
            dataType: "json",
            success: function(data) {
                console.log(data);
	        	temp_html = "";
	            for(var i in data){
	            	temp_html+="<option value='"+i+"' data-id='"+data[i]+"'>"+i+"</option>";
	            }
	            oCountry.html(temp_html);
            }
        });
    };

    oProvince.change(function(){
        city();
    });

    oCity.change(function(){
        district();
    });

    $('#Edit_Record').validate({
        onkeyup: false
    });

    $('.partners').slideUp();
});

function SetValidator() {
    $('.admin-email').each(function() {
        var adminField = $(this).attr('data-admin-id');
        var adminId = $(adminField).val();
        $(this).rules("add", {
            uniqueEmail: {
                id: adminId,
                table: "admin"
            }
        });
    });
}

function saveDraft(id_name, objId_name, publish_name, field_name, preview) {
    if ($('#Edit_Record').valid()) {
        $('body').css('cursor', 'wait');
        $('#' + publish_name).val('0');
        var id_key0 = encodeURIComponent(id_name + '[0]');
        var id_key1 = encodeURIComponent(id_name + '[1]');
        var objId_key = encodeURIComponent($('#' + objId_name).attr('name'));
        var publish_key = encodeURIComponent($('#' + publish_name).attr('name'));
        var field_key = encodeURIComponent(field_name);
        var field_value = encodeURIComponent(mysql_now());
        $.ajax({
            type: "POST",
            url: "/admin/includes/processes/processes-record.php",
            cache: false,
            async: false,
            data: id_key0 + '=' + objId_name + '&' + id_key1 + '=' + publish_name + '&' + objId_key + "=" + $('#' + objId_name).val() + "&" + publish_key + "=0&" + field_key + "=" + field_value + '&formToken=' + $('#formToken').val(),
            dataType: "html",
            success: function(data, textStatus) {
                try {
                    var obj = $.parseJSON(data);
                    if (obj.notice) {
                        $('.key').val('');
                        $('#Edit_Record').submit();
                        $('.published').hide();
                        $('.drafts').show();
                        // 						buildUrl('tbl_franchisee','franchisee_parent_id',objId_name, preview);
                    }
                } catch (err) {}
                $('body').css('cursor', 'default');
            }
        });
        $('body').css('cursor', 'default');
    }
}

function publish(id_name, objId_name, publish_name, field_name) {
    if ($('#Edit_Record').valid()) {
        $('body').css('cursor', 'wait');
        $('#' + publish_name).val('1');
        var id_key0 = encodeURIComponent(id_name + '[0]');
        var id_key1 = encodeURIComponent(id_name + '[1]');
        var objId_key = encodeURIComponent($('#' + objId_name).attr('name'));
        var publish_key = encodeURIComponent($('#' + publish_name).attr('name'));
        var field_key = encodeURIComponent(field_name);
        var field_value = encodeURIComponent(mysql_now());
        $.ajax({
            type: "POST",
            url: "/admin/includes/processes/processes-record.php",
            cache: false,
            data: id_key0 + '=' + objId_name + '&' + id_key1 + '=' + publish_name + '&' + objId_key + "=" + $('#' + objId_name).val() + "&" + publish_key + "=1&" + field_key + "=" + field_value + '&formToken=' + $('#formToken').val(),
            dataType: "html",
            success: function(data, textStatus) {
                try {
                    var obj = $.parseJSON(data);
                    if (obj.notice) {
                        $('#Edit_Record').submit();
                        $('.drafts').hide();
                        $('.published').show();
                    }
                } catch (err) {}
                $('body').css('cursor', 'default');
            }
        });
        $('body').css('cursor', 'default');
    }
}

function unpublish(publish_name) {
    $('#' + publish_name).val('0');
    $('#Edit_Record').submit();
    $('.published').hide();
    $('.drafts').show();
}


function newPartner() {
    $('body').css('cursor', 'wait');
    var no = $('#partnerno').val();
    no++;
    $('#partnerno').val(no);
    $.ajax({
        type: "POST",
        url: "/admin/includes/processes/load-template.php",
        cache: false,
        data: "template=franchise_users.tpl&partnerno=" + no,
        dataType: "html",
        success: function(data, textStatus) {
            try {
                $('#partners-wrapper').append(data);
                $('body').css('cursor', 'default');
                scrolltodiv('#partner_wrapper' + no);
            } catch (err) {
                $('body').css('cursor', 'default');
            }
        }
    });
}

function togglePartner(ID) {
    if ($('#partner' + ID).is(':visible')) {
        $('.partners').slideUp();
    } else {
        $('.partners').slideUp();
        $('#partner' + ID).slideDown();
    }
}

function deletePartner(ID) {
    if (ConfirmDelete()) {
        var count = $('#' + ID).attr('rel');
        var today = mysql_now();

        html = '<input type="hidden" value="' + today + '" name="field[20][tbl_access][' + count + '][access_deleted]" /><input type="hidden" value="' + today + '" name="field[10][tbl_admin][' + count + '][admin_deleted]" />';
        $('#' + ID).append(html);
        $('#' + ID).css('display', 'none');
        $('#' + ID).removeClass('partners');
    } else {
        return false;
    }
}

function createPassword(emailID, pwdID, resPwdID) {
    if ($('#' + emailID).val() != '' && $('#' + pwdID).val() != '') {
        $.ajax({
            type: "POST",
            url: "/admin/includes/processes/createPass.php",
            cache: false,
            data: "username=" + $('#' + emailID).val() + "&password=" + $('#' + pwdID).val(),
            dataType: "json",
            success: function(res, textStatus) {
                try {
                    $('#' + resPwdID).val(res.password);
                } catch (err) {}
            }
        });
    } else {
        var init_pass = $('#' + resPwdID).attr('data-password');
        $('#' + resPwdID).val(init_pass);
    }
}

function setFormCallback(NUMBER) {
    $('#Edit_Record').attr('data-element', NUMBER).attr('data-callback', 'SaveNotify').submit();
}

function SaveNotify() {
    var number = $('#Edit_Record').attr('data-element');
    var email = encodeURIComponent($('#admin_email_' + number).val());
    var admin_id = encodeURIComponent($('#admin_id_' + number).val());

    if (email && admin_id) {

        var datastring = 'action=NotifyFranchiseUser&email=' + email + '&admin_id=' + admin_id + '&formToken=' + $('#formToken').val();
        $('body').css('cursor', 'wait');
        $('#send-btn').addClass('disabled');
        $.ajax({
            type: "POST",
            url: "/admin/includes/processes/processes-email.php",
            cache: false,
            async: false,
            data: datastring,
            dataType: "html",
            success: function(data) {
                try {
                    var obj = $.parseJSON(data);
                    if (obj.response) {
                        $('#sent').slideDown();
                        setTimeout(function() {
                            $('#sent').slideUp();
                        }, 10000);
                    } else {
                        $('#error').slideDown();
                        setTimeout(function() {
                            $('#error').slideUp();
                        }, 10000);
                    }
                    $('#Edit_Record').removeAttr('data-callback');
                } catch (err) {
                    console.log('TRY-CATCH error');
                    $('#Edit_Record').removeAttr('data-callback');
                }
                $('body').css('cursor', 'default');
                $('#send-btn').removeClass('disabled');
            },
            error: function() {
                $('#Edit_Record').removeAttr('data-callback');
                $('body').css('cursor', 'default');
                $('#send-btn').removeClass('disabled');
                console.log('AJAX error');
            }
        });
    }
}

function fillLocation() {}
</script>
{/block}
